{include file="header.tpl"}
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
  $(".delete").live('click', function(event) 
  {
    $(this).parent().remove();
  });
  $('#totVadhGhat').val("{$totVadhGhat}");
});

function addSetVadhGhat(txtObj)
{
	var row = $(txtObj).parents('.kaarigarRow');
	var dr = isNaN(parseFloat(row.find('.addDebit').val())) ? 0 : parseFloat(row.find('.addDebit').val());
	var cr = isNaN(parseFloat(row.find('.addCredit').val())) ? 0 : parseFloat(row.find('.addCredit').val());
  var price = cr - dr;
  isNaN(price) ? row.find('.addVadhaaroGhataado').val("") : row.find('.addVadhaaroGhataado').val(price);
  totalVadhGhat();
  setVadhGhat();
}

function setVadhGhat(txtObj)
{
	var row = $(txtObj).parents('.kaarigarRow');
	var cr = isNaN(parseFloat(row.find('.credit').val())) ? 0 : parseFloat(row.find('.credit').val());
	var dr = isNaN(parseFloat(row.find('.debit').val())) ? 0 : parseFloat(row.find('.debit').val());
  var price = cr - dr;
  isNaN(price) ? row.find('.vadhaaroGhataado').val("") : row.find('.vadhaaroGhataado').val(price);
  totalVadhGhat();
}

function totalVadhGhat()
{
	var totVadhGhat = 0;
	var totCrAmount = 0;
	var totDrAmount = 0;
  
  $('.vadhaaroGhataado').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totVadhGhat += Number(price);
  });
  $('#totVadhGhat').val(totVadhGhat);
  
  $('.crAmount').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totCrAmount += Number(price);
  });
  $('#totCrAmount').val(totCrAmount);
  
  $('.drAmount').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totDrAmount += Number(price);
  });
  $('#totDrAmount').val(totDrAmount);
}

function setAmount(txtObj)
{
  var row = $(txtObj).parents('.kaarigarRow');
  var price = parseFloat(row.find('.qty').val()) * parseFloat(row.find('.rate').val());
  isNaN(price) ? row.find('.crAmount').val("") : row.find('.crAmount').val(price);
}

function addNewRow()
{
  $('#kaarigarTable tr:last').after('<tr class="kaarigarRow"><td><input type="text" name="particulars[]" class="particulars"\/><\/td><td><input type="text" name="debit[]" size="8" class="debit" onChange="setVadhGhat(this)" \/><\/td><td><input type="text" name="credit[]" size="8" class="credit" onChange="setVadhGhat(this)" \/><\/td><td><input type="text" name="particular[]" class="particular"\/><\/td><td><input type="text" name="vadhaaroGhataado[]" size="8" class="vadhaaroGhataado" onChange="totalVadhGhat()" \/><\/td><td><input type="text" name="addDebit[]" size="8" class="addDebit" onChange="addSetVadhGhat(this)" \/><\/td><td><input type="text" name="addCredit[]" size="8" class="addCredit" onChange="addSetVadhGhat(this)" \/><\/td><td><input type="hidden" name="addVadhaaroGhataado[]" size="8" class="addVadhaaroGhataado" onChange="addSetVadhGhat(this)" \/><\/td><td><input type="text" name="qty[]" size="8" class="qty" onChange="setAmount(this)" \/><\/td><td><input type="text" name="rate[]" size="8" class="rate" onChange="setAmount(this)" \/><\/td><td><input type="text" name="crAmount[]" size="8" class="crAmount" onChange="addSetVadhGhat()" \/></td><td><input type="text" name="drAmount[]"size="8" class="drAmount" onChange="addSetVadhGhat()" \/><\/td><td><a href="javascript:addNewRow();">{$l_add}<\/a><\/td><td class="delete">{$l_remove}<\/td><\/tr>');
}
</script>
<form name="form1" action="" method="post">
<input type="hidden" name="kaarigarMasterId" value="{$kaarigarMasterId}" />
<input type="hidden" name="bdate" id="bdate" value="0" />
<input type="hidden" name="isEdit" value="{$isEdit}" />
<center><h4>{$l_kaarigarEntry}</h4></center>
<div class="contentDiv">
  <table border="0" id="kaarigarTable" align="center" width="100%">
  <tr>
    <td colspan="4" class="table1">
      {$l_kaarigarName} :
      <select name="kaarigarId">
        {html_options values=$kaarigar.id output=$kaarigar.name selected=$kaarigarDetail[0].kaarigarId}
      </select>
    </td>
    <td  colspan="10" class="table1">
      {$l_date} : 
      {html_select_date prefix="curDate" start_year="-2" end_year="+1" field_order="DMY" day_value_format="%02d" time=$kaarigarDetail[0].kaarigarDate}
    </td>
  </tr>
  <tr class="table1">
    <th>Parti</th>
    <th>{$l_debit}</th>
    <th>{$l_credit}</th>
    <th>Parti</th>
    <th>{$l_vadhaaroGhataado}</th>
    <th>Ad.J</th>
    <th>Ad.A</th>
    <th>&nbsp;</th>
    <th>{$l_qty}</th>
    <th>{$l_rate}</th>
    <th>Cr Amt</th>
    <th>Dr Amt</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
  </tr>
  {section name="sec" loop=$kaarigarDetail|@count}
	  <tr class="kaarigarRow">
	    <td><input type="text" name="particulars[]" value="{$kaarigarDetail[sec].particulars}" /></td>
	    <td><input type="text" name="debit[]" size="8" class="debit" onChange="setVadhGhat(this)" value="{$kaarigarDetail[sec].debit}" /></td>
	    <td><input type="text" name="credit[]" size="8" class="credit" onChange="setVadhGhat(this)" value="{$kaarigarDetail[sec].credit}" /></td>
      <td><input type="text" name="particular[]" value="{$kaarigarDetail[sec].particular}" /></td>
	    <td><input type="text" name="vadhaaroGhataado[]" size="8" class="vadhaaroGhataado" value="{$kaarigarDetail[sec].vadhaaroGhatado}" /></td>
	    <td><input type="text" name="addDebit[]" size="8" class="addDebit" onChange="addSetVadhGhat(this)" value="{$kaarigarDetail[sec].addDebit}" /></td>
	    <td><input type="text" name="addCredit[]" size="8" class="addCredit" onChange="addSetVadhGhat(this)" value="{$kaarigarDetail[sec].addCredit}" /></td>
	    <td><input type="hidden" name="addVadhaaroGhataado[]" size="8" class="addVadhaaroGhataado" value="{$kaarigarDetail[sec].addVadhaaroGhataado}" /></td>
	    <td><input type="text" name="qty[]" size="8" class="qty" onChange="setAmount(this)" value="{$kaarigarDetail[sec].qty}" /></td>
	    <td><input type="text" name="rate[]" size="8" class="rate" onChange="setAmount(this)" value="{$kaarigarDetail[sec].rate}" /></td>
	    <td><input type="text" name="crAmount[]" size="8" class="crAmount" value="{$kaarigarDetail[sec].crAmount}" /></td>
	    <td><input type="text" name="drAmount[]" size="8" class="drAmount" value="{$kaarigarDetail[sec].drAmount}" /></td>
	    <td><a href="javascript:addNewRow();">{$l_add}</a></td>
	    <td class="delete">{$l_remove}</td>
	  </tr>
	{/section}
	<tr class="kaarigarRow">
    <td><input type="text" name="particulars[]"/></td>
    <td><input type="text" name="debit[]"     size="8" class="debit" onChange="setVadhGhat(this)" /></td>
    <td><input type="text" name="credit[]"    size="8" class="credit" onChange="setVadhGhat(this)" /></td>
    <td><input type="text" name="particular[]" /></td>
    <td><input type="text" name="vadhaaroGhataado[]" size="8" class="vadhaaroGhataado" onChange="totalVadhGhat()" /></td>
    <td><input type="text" name="addDebit[]"  size="8" class="addDebit" onChange="addSetVadhGhat(this)" /></td>
    <td><input type="text" name="addCredit[]" size="8" class="addCredit" onChange="addSetVadhGhat(this)" /></td>
    <td><input type="hidden" name="addVadhaaroGhataado[]" size="8" class="addVadhaaroGhataado" onChange="setVadhGhat()" /></td>
    <td><input type="text" name="qty[]"       size="8" class="qty" onChange="setAmount(this)" /></td>
    <td><input type="text" name="rate[]"      size="8" class="rate" onChange="setAmount(this)" /></td>
    <td><input type="text" name="crAmount[]"  size="8" class="crAmount" onChange="addSetVadhGhat()" /></td>
    <td><input type="text" name="drAmount[]"  size="8" class="drAmount" onChange="addSetVadhGhat()"/></td>
    <td><a href="javascript:addNewRow();">{$l_add}</a></td>
    <td class="delete">{$l_remove}</td>
  </tr>
  </table>
  <table width="100%">
  	<tr class="itemRowTot">
	  	<td width="130px;">&nbsp;</td>
	  	<td width="130px;">&nbsp;</td>
	  	<td width="130px;">{$l_total}</td>
	  	<td><input type="text" name="totalVadhGhat" id="totVadhGhat" /></td>
	  	<td width="130px;">&nbsp;</td>
	  	<td width="130px;">&nbsp;</td>
	  	<td width="130px;">&nbsp;</td>
	  	<td><input type="text" name="totCrAmount" id="totCrAmount" /></td>
	  	<td><input type="text" name="totDrAmount" id="totDrAmount" /></td>
	  </tr>
  </table>
  <input type="submit" name="submitBtn" value="{if $kaarigarDetail|@count > 0} {$l_save} {else} {$l_add} {/if}" />
</div>
</form>
{include file="footer.tpl"}